package net.web.controller;

import net.web.common.framework.simplemvc.RequestMapping;
import net.web.common.utils.DateUtils;
import net.web.common.utils.JSONUtil;
import net.web.common.utils.PageInfo;
import net.web.common.utils.Result;
import net.web.entity.Class;
import net.web.service.ClassService;
import net.web.service.impl.ClassServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RequestMapping("/api/class")
public class ClassController {

    private ClassService classService = new ClassServiceImpl();

    @RequestMapping("/pageList.do")
    public void pageList(HttpServletRequest req, HttpServletResponse resp) {
        String pageNoStr   = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        int pageNo   = pageNoStr == null || pageNoStr.isEmpty()   ? 1 : Integer.parseInt(pageNoStr);
        int pageSize = pageSizeStr == null || pageSizeStr.isEmpty() ? 10 : Integer.parseInt(pageSizeStr);

        Class cls = new Class();
        cls.setName(req.getParameter("name"));
        cls.setIsfeatured(parseInt(req.getParameter("isfeatured"), -1));

        PageInfo<Class> pageInfo = classService.pageList(pageNo, pageSize, cls);
        JSONUtil.printByJSON(resp, Result.success(pageInfo));
    }

    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) {
        List<Class> list = classService.list();
        JSONUtil.printByJSON(resp, Result.success(list));
    }

    @RequestMapping("/insert.do")
    public void insert(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Class cls = new Class();
        cls.setName(req.getParameter("name"));
        cls.setDescription(req.getParameter("description"));
        cls.setParentid(parseInt(req.getParameter("parentid"), 0));
        cls.setCoverimage(req.getParameter("coverimage"));
        cls.setSortorder(parseInt(req.getParameter("sortorder"), 0));
        cls.setIsfeatured(parseInt(req.getParameter("isfeatured"), 0));

        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        cls.setCreatedtime(now);
        cls.setUpdatedtime(now);

        boolean ok = classService.insert(cls);
        JSONUtil.printByJSON(resp, ok ? Result.success("添加成功") : Result.fail("添加失败"));
    }

    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        boolean ok = classService.delete(id);
        JSONUtil.printByJSON(resp, ok ? Result.success("删除成功") : Result.fail("删除失败"));
    }

    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Class cls = new Class();
        cls.setId(Integer.parseInt(req.getParameter("id")));

        String name = req.getParameter("name");
        if (name != null && !name.trim().isEmpty()) cls.setName(name);

        String desc = req.getParameter("description");
        if (desc != null) cls.setDescription(desc);

        String cover = req.getParameter("coverimage");
        if (cover != null) cls.setCoverimage(cover);

        cls.setParentid(parseInt(req.getParameter("parentid"), cls.getParentid()));
        cls.setSortorder(parseInt(req.getParameter("sortorder"), cls.getSortorder()));
        cls.setIsfeatured(parseInt(req.getParameter("isfeatured"), cls.getIsfeatured()));
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        cls.setUpdatedtime(now);

        boolean ok = classService.update(cls);
        JSONUtil.printByJSON(resp, ok ? Result.success("更新成功") : Result.fail("更新失败"));
    }

    private int parseInt(String val, int defaultVal) {
        if (val == null || val.trim().isEmpty()) return defaultVal;
        try { return Integer.parseInt(val.trim()); }
        catch (NumberFormatException e) { return defaultVal; }
    }
}